package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.entity.order.OrderNoInfo;
import cn.buk.tms.sc.SearchSmsCriteria;
import cn.buk.qms.service.FlightChangeOrderService;
import cn.buk.qms.service.FlightOrderService;
import cn.buk.qms.service.FlightRefundOrderService;
import cn.buk.qms.service.SmsService;
import cn.buk.tms.dto.SmsDetailDto;
import cn.buk.tms.train.service.TrainOrderService;
import cn.buk.common.util.DateUtil;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

import static cn.buk.tms.common.constant.TmsOrderConstant.*;


/**
 * @author yfdai
 */
@RestController
@RequestMapping("/sms")
public class SmsController extends BaseController {

  private static final Logger logger = LogManager.getLogger(SmsController.class);

  private final SmsService service;

  private final FlightOrderService flightOrderService;

  private final FlightRefundOrderService refundOrderService;

  private final FlightChangeOrderService changeOrderService;

  private final TrainOrderService trainOrderService;

  @Autowired
  public SmsController(SmsService service,
                       FlightOrderService flightOrderService,
                       FlightRefundOrderService refundOrderService,
                       FlightChangeOrderService changeOrderService,
                       TrainOrderService trainOrderService) {
    this.service = service;
    this.flightOrderService = flightOrderService;
    this.refundOrderService = refundOrderService;
    this.changeOrderService = changeOrderService;
    this.trainOrderService = trainOrderService;
  }

  /**
   * 查找
   */
  @RequestMapping("/")
  public CommonDto<SmsDetailDto> searchSmsList(HttpServletRequest request,
                                               @ModelAttribute("sc") SearchSmsCriteria sc) {
    final int enterpriseId = getEnterpriseId(request);

    List<SmsDetailDto> details = service.searchSmsDetails(enterpriseId, getUserId(request), getUsername(request), sc);

    CommonDto<SmsDetailDto> commonDto = new CommonDto<>();
    commonDto.setDataList(details);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 接收上行短信
   *
   * @param enterpriseId
   * @param recvtime     - 接收时间 2012-12-11 13:25:36 去掉所有间隔后的时间串：20121211132536
   * @param mobile       发送的手机号
   * @param content      发送的内容
   * @return
   */
  @RequestMapping("/{id}/receive")
  public int receiveSms(@PathVariable("id") final int enterpriseId,
                        @RequestParam(value = "recvtime", required = false) String recvtime,
                        @RequestParam(value = "mobile", required = false) String mobile,
                        @RequestParam(value = "content", required = false) String content
  ) {
    logger.info("sms received: " + mobile + ", " + recvtime + ", " + content);

    service.saveReceivedSms(enterpriseId, mobile, content);

    return 0;
  }

  /**
   * 获取订单发送短信的内容
   */
  @GetMapping(value = "/order/content")
  public JsonResult getOrderSmsContent(HttpServletRequest request,
                                       @RequestParam(value = "orderType", defaultValue = ORDER_TYPE_FLIGHT + "") final int orderType,
                                       @RequestParam("orderId") int orderId,
                                       @RequestParam(value = "psgId", defaultValue = "0") final int psgId,
                                       @RequestParam(value = "smsFormat", defaultValue = "0") final int smsFormat // 短信格式
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    if (orderType == ORDER_TYPE_FLIGHT) {
      //机票订单的短信
      return flightOrderService
              .getFlightOrderSmsContent(enterpriseId, username, orderId, psgId, smsFormat);

    } else if (orderType == ORDER_TYPE_FLIGHT_REFUND) {
      //退票订单的短信内容
      return refundOrderService
              .getFlightRefundOrderSmsContent(enterpriseId, username, orderId, smsFormat);

    } else if (orderType == ORDER_TYPE_FLIGHT_CHANGE) {
      //改签单的短信内容
      return changeOrderService
              .getFlightChangeOrderSmsContent(enterpriseId, username, orderId, smsFormat);

    } else if (orderType == ORDER_TYPE_TRAIN) {
      //火车票订单
      return trainOrderService
              .getTrainOrderOrderSmsContent(enterpriseId, username, orderId, psgId, smsFormat);

    } else {
      return JsonResult.createJsonResult(-1, "该短信内容还在建设中... " + DateUtil.getCurDateTimeString());
    }
  }

  /**
   * 根据订单发送短信
   */
  @PostMapping(value = "/order/content")
  public JsonResult sendOrderSmsContent(HttpServletRequest request,
                                        @RequestParam(value = "orderType", defaultValue = OrderNoInfo.ORDER_FLIGHT) final String orderType,
                                        @RequestParam("orderId") int orderId,
                                        @RequestParam(value = "psgId", defaultValue = "0") final int psgId,
                                        @RequestParam(value = "smsFormat", defaultValue = "0") final int smsFormat, // 短信格式,
                                        @RequestParam(value = "mobileNo", defaultValue = "") String mobileNo,
                                        @RequestParam(value = "smsContent", required = false) String smsContent
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    if (orderType.equalsIgnoreCase(OrderNoInfo.ORDER_FLIGHT)) {

      return flightOrderService
              .sendFlightOrderSms(enterpriseId, username, orderId, psgId, mobileNo, smsContent,
                      smsFormat);

    } else if (orderType.equalsIgnoreCase(OrderNoInfo.ORDER_FLIGHT_REFUND)) {
      //退票订单的短信内容
      return refundOrderService
              .sendFlightRefundOrderSms(enterpriseId, username, orderId, mobileNo, smsFormat,
                      smsContent);

    } else if (orderType.equalsIgnoreCase(OrderNoInfo.ORDER_FLIGHT_CHANGE)) {
      //改签单的短信内容
      return changeOrderService
              .sendFlightChangeOrderSms(enterpriseId, username, orderId, mobileNo, smsFormat,
                      smsContent);
    } else if (orderType.equalsIgnoreCase(OrderNoInfo.ORDER_TRAIN)) {
      //火车票订单
      return trainOrderService
              .sendTrainOrderOrderSmsContent(enterpriseId, username, orderId, psgId, mobileNo,
                      smsFormat, smsContent);
    } else {
      return JsonResult.createJsonResult(-1, "该短信内容还在建设中... " + DateUtil.getCurDateTimeString());
    }
  }

}
